package com.mangowang.module.main.ui;

/**
 * 文件名 : EarningDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.lib.common.proto.Transfer;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.lib.common.util.ValidateUtils;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.ruffian.library.widget.RLinearLayout;


import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllowActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_name)
    TextView tvName;

    @BindView(R2.id.tv_number)
    TextView tvNumber;
    @BindView(R2.id.btn_submit)
    Button btnSubmit;
    @BindView(R2.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R2.id.tv_ban)
    TextView tv_ban;
    @BindView(R2.id.tv_pai)
    TextView tv_pai;
    @BindView(R2.id.rg_group)
    RadioGroup rg_group;

    @BindView(R2.id.rv_jiang_li)
    RelativeLayout rv_jiang_li;

    private String sn_up_code = "";
    private String ban = "";
    private String pai = "";

    private String phone = "";
    private String setting = "1";
    private String id = "";
    private String number = "0";
    private String goodsid = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allow);
        unbinder = ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        tvTitle.setText("终端划拨");
        backIv.setOnClickListener(v -> finish());
        initListener();
        initDatas();
    }

    private void initDatas() {
        sn_up_code = getIntent().getStringExtra("sn_up_code");
        ban = getIntent().getStringExtra("ban");
        pai = getIntent().getStringExtra("pai");
        goodsid = getIntent().getStringExtra("goodsid");
        number = getIntent().getStringExtra("number");
        tv_ban.setText(ban);
        tv_pai.setText(pai);
        tvNumber.setText("已选" + number + "个终端");
    }

    private void initListener() {


        backIv.setOnClickListener(v -> {
            finish();
        });


        btnSubmit.setOnClickListener(view -> submit());

        tvName.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, AllocaPartnerActivity.class);
            intent.putExtra("title", "选择接收伙伴");
            intent.putExtra("type", "2");
            startActivityForResult(intent, 100);
        });


        rg_group.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_one) setting = "0";
            if (checkedId == R.id.rb_two) setting = "1";
        });

    }


    private void submit() {
        String name = tvName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请先选接收伙伴");
            return;
        }

        Transfer.transfer.Builder builder = Transfer.transfer.newBuilder();
        builder.setSnCode(sn_up_code).setMemberName(name).setGoodsId(goodsid)
                .setMemberMobile(id).setNum(number).setSetting(setting);
        showProgress();
        btnSubmit.setEnabled(false);
        NetworkManager.INSTANCE.post(Apis.transfer_sn, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {
                hideProgress();
                btnSubmit.setEnabled(true);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
                showToast(errorMessage);
                btnSubmit.setEnabled(true);
                if (errorCode == 200) {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            tvName.setText(data.getStringExtra("name"));
            phone = data.getStringExtra("phone");
            id = data.getStringExtra("id");
        }
    }
}
